<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>新增地址 - 邦伴用户端</title>
  <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
  <style>
    :root {
      --primary: #ff7e2d;
      --primary-light: #fff3ea;
      --border: #f0f0f0;
      --text-main: #222;
      --text-sub: #888;
    }
    
    body {
      font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
      background-color: #f8f8f8;
      color: var(--text-main);
    }
    
    .app-container {
      width: 375px;
      height: 812px;
      margin: 0 auto;
      background: #fff;
      position: relative;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.05);
      overflow-y: auto;
    }
    
    .primary-color {
      color: var(--primary);
    }
    
    .primary-bg {
      background-color: var(--primary);
    }
    
    .primary-light-bg {
      background-color: var(--primary-light);
    }
    
    .btn-primary {
      background-color: var(--primary);
      color: white;
    }
    
    .btn-outline {
      border: 1px solid var(--primary);
      color: var(--primary);
    }
    
    .form-group {
      margin-bottom: 16px;
    }
    
    .form-label {
      display: block;
      margin-bottom: 8px;
      font-size: 14px;
      color: var(--text-sub);
    }
    
    .form-input {
      width: 100%;
      padding: 12px;
      border: 1px solid var(--border);
      border-radius: 8px;
      font-size: 14px;
    }
    
    .form-input:focus {
      outline: none;
      border-color: var(--primary);
    }
    
    .form-select {
      width: 100%;
      padding: 12px;
      border: 1px solid var(--border);
      border-radius: 8px;
      font-size: 14px;
      background-color: white;
      appearance: none;
      background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='%23888' viewBox='0 0 16 16'%3E%3Cpath d='M7.247 11.14 2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z'/%3E%3C/svg%3E");
      background-repeat: no-repeat;
      background-position: right 12px center;
    }
    
    .form-select:focus {
      outline: none;
      border-color: var(--primary);
    }
    
    .form-textarea {
      width: 100%;
      padding: 12px;
      border: 1px solid var(--border);
      border-radius: 8px;
      font-size: 14px;
      resize: none;
      min-height: 100px;
    }
    
    .form-textarea:focus {
      outline: none;
      border-color: var(--primary);
    }
    
    .switch {
      position: relative;
      display: inline-block;
      width: 44px;
      height: 24px;
    }
    
    .switch input {
      opacity: 0;
      width: 0;
      height: 0;
    }
    
    .slider {
      position: absolute;
      cursor: pointer;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: #ccc;
      transition: .4s;
      border-radius: 24px;
    }
    
    .slider:before {
      position: absolute;
      content: "";
      height: 18px;
      width: 18px;
      left: 3px;
      bottom: 3px;
      background-color: white;
      transition: .4s;
      border-radius: 50%;
    }
    
    input:checked + .slider {
      background-color: var(--primary);
    }
    
    input:focus + .slider {
      box-shadow: 0 0 1px var(--primary);
    }
    
    input:checked + .slider:before {
      transform: translateX(20px);
    }
  </style>
</head>
<body>
  <div class="app-container">
    <!-- 顶部导航栏 -->
    <div class="sticky top-0 z-10 bg-white border-b border-gray-100 px-4 py-3 flex items-center">
      <a href="javascript:history.back()" class="mr-2">
        <i class="fas fa-arrow-left text-lg"></i>
      </a>
      <h1 class="text-lg font-medium flex-1 text-center">新增地址</h1>
    </div>
    
    <!-- 地址表单 -->
    <div class="p-4">
      <form id="addressForm">
        <div class="form-group">
          <label class="form-label">收货人</label>
          <input type="text" class="form-input" id="name" name="name" placeholder="请输入收货人姓名">
        </div>
        
        <div class="form-group">
          <label class="form-label">手机号码</label>
          <input type="tel" class="form-input" id="phone" name="phone" placeholder="请输入手机号码">
        </div>
        
        <div class="form-group">
          <label class="form-label">所在地区</label>
          <div class="flex space-x-2">
            <select class="form-select" id="province" name="province">
              <option value="">选择省份</option>
              <option value="上海市">上海市</option>
              <option value="北京市">北京市</option>
              <option value="广东省">广东省</option>
              <option value="江苏省">江苏省</option>
              <option value="浙江省">浙江省</option>
            </select>
            <select class="form-select" id="city" name="city" disabled>
              <option value="">选择城市</option>
            </select>
            <select class="form-select" id="district" name="district" disabled>
              <option value="">选择区县</option>
            </select>
          </div>
        </div>
        
        <div class="form-group">
          <label class="form-label">详细地址</label>
          <textarea class="form-textarea" id="address" name="address" placeholder="请输入详细地址，如街道、门牌号等"></textarea>
        </div>
        
        <div class="form-group">
          <label class="form-label">地址标签</label>
          <div class="flex space-x-2">
            <button type="button" class="px-3 py-1 rounded-full bg-gray-100 text-sm tag-btn" data-tag="家">家</button>
            <button type="button" class="px-3 py-1 rounded-full bg-gray-100 text-sm tag-btn" data-tag="公司">公司</button>
            <button type="button" class="px-3 py-1 rounded-full bg-gray-100 text-sm tag-btn" data-tag="学校">学校</button>
            <button type="button" class="px-3 py-1 rounded-full bg-gray-100 text-sm tag-btn" data-tag="其他">其他</button>
          </div>
          <input type="hidden" id="tag" name="tag" value="">
        </div>
        
        <div class="form-group flex justify-between items-center">
          <span>设为默认地址</span>
          <label class="switch">
            <input type="checkbox" id="isDefault" name="isDefault">
            <span class="slider"></span>
          </label>
        </div>
        
        <button type="submit" class="w-full py-3 btn-primary text-center rounded-lg mt-6">保存</button>
      </form>
    </div>
  </div>

  <script>
    document.addEventListener('DOMContentLoaded', function() {
      // 地区联动选择
      const provinceSelect = document.getElementById('province');
      const citySelect = document.getElementById('city');
      const districtSelect = document.getElementById('district');
      
      // 省份变化时更新城市
      provinceSelect.addEventListener('change', function() {
        if (this.value) {
          citySelect.disabled = false;
          
          // 清空并重置城市选项
          citySelect.innerHTML = '<option value="">选择城市</option>';
          districtSelect.innerHTML = '<option value="">选择区县</option>';
          districtSelect.disabled = true;
          
          // 根据选择的省份加载对应的城市
          // 这里只是示例，实际应该从数据库或API获取
          if (this.value === '上海市') {
            addOption(citySelect, '上海市', '上海市');
          } else if (this.value === '北京市') {
            addOption(citySelect, '北京市', '北京市');
          } else if (this.value === '广东省') {
            addOption(citySelect, '广州市', '广州市');
            addOption(citySelect, '深圳市', '深圳市');
            addOption(citySelect, '东莞市', '东莞市');
          } else if (this.value === '江苏省') {
            addOption(citySelect, '南京市', '南京市');
            addOption(citySelect, '苏州市', '苏州市');
            addOption(citySelect, '无锡市', '无锡市');
          } else if (this.value === '浙江省') {
            addOption(citySelect, '杭州市', '杭州市');
            addOption(citySelect, '宁波市', '宁波市');
            addOption(citySelect, '温州市', '温州市');
          }
        } else {
          citySelect.disabled = true;
          districtSelect.disabled = true;
        }
      });
      
      // 城市变化时更新区县
      citySelect.addEventListener('change', function() {
        if (this.value) {
          districtSelect.disabled = false;
          
          // 清空并重置区县选项
          districtSelect.innerHTML = '<option value="">选择区县</option>';
          
          // 根据选择的城市加载对应的区县
          // 这里只是示例，实际应该从数据库或API获取
          if (this.value === '上海市') {
            addOption(districtSelect, '浦东新区', '浦东新区');
            addOption(districtSelect, '黄浦区', '黄浦区');
            addOption(districtSelect, '徐汇区', '徐汇区');
            addOption(districtSelect, '长宁区', '长宁区');
            addOption(districtSelect, '静安区', '静安区');
          } else if (this.value === '北京市') {
            addOption(districtSelect, '东城区', '东城区');
            addOption(districtSelect, '西城区', '西城区');
            addOption(districtSelect, '朝阳区', '朝阳区');
            addOption(districtSelect, '海淀区', '海淀区');
          }
        } else {
          districtSelect.disabled = true;
        }
      });
      
      // 添加选项辅助函数
      function addOption(select, value, text) {
        const option = document.createElement('option');
        option.value = value;
        option.textContent = text;
        select.appendChild(option);
      }
      
      // 标签选择
      const tagButtons = document.querySelectorAll('.tag-btn');
      const tagInput = document.getElementById('tag');
      
      tagButtons.forEach(button => {
        button.addEventListener('click', function() {
          // 移除所有标签的激活状态
          tagButtons.forEach(btn => {
            btn.classList.remove('active');
            btn.classList.remove('primary-bg');
            btn.classList.remove('text-white');
            btn.classList.add('bg-gray-100');
          });
          
          // 激活当前标签
          this.classList.add('active');
          this.classList.add('primary-bg');
          this.classList.add('text-white');
          this.classList.remove('bg-gray-100');
          
          // 设置隐藏输入值
          tagInput.value = this.dataset.tag;
        });
      });
      
      // 表单提交
      const addressForm = document.getElementById('addressForm');
      addressForm.addEventListener('submit', function(e) {
        e.preventDefault();
        
        // 表单验证
        const name = document.getElementById('name').value;
        const phone = document.getElementById('phone').value;
        const province = document.getElementById('province').value;
        const city = document.getElementById('city').value;
        const district = document.getElementById('district').value;
        const address = document.getElementById('address').value;
        
        if (!name) {
          alert('请输入收货人姓名');
          return;
        }
        
        if (!phone) {
          alert('请输入手机号码');
          return;
        }
        
        if (!province || !city || !district) {
          alert('请选择完整的所在地区');
          return;
        }
        
        if (!address) {
          alert('请输入详细地址');
          return;
        }
        
        // 获取表单数据
        const formData = new FormData(this);
        const data = {};
        for (let [key, value] of formData.entries()) {
          data[key] = value;
        }
        
        // 添加是否默认地址
        data.isDefault = document.getElementById('isDefault').checked;
        
        console.log('提交的地址数据:', data);
        
        // 这里可以添加AJAX请求来保存地址
        // 保存成功后跳转回地址列表页面
        window.location.href = 'address_manage.html';
      });
    });
  </script>
</body>
</html>